package com.atguigu.gulimall.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.Feign.SeckillFeignClient;
import com.atguigu.gulimall.product.entity.*;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.SeckillInfoVo;
import com.atguigu.gulimall.product.vo.SkuItemSaleAttrsVo;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    private ThreadPoolExecutor poolExecutor;
    @Autowired
    SeckillFeignClient seckillFeignClient;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkuInfoBySpuId(Long spuId) {
        List<SkuInfoEntity> skuInfoEntities = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return skuInfoEntities;
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo item = new SkuItemVo();

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            //2.sku的图片信息 pms_sku_images
            List<SkuImagesEntity> imagesEntities = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            item.setImages(imagesEntities);
        }, poolExecutor);

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //1.sku基本信息获取 pms_sku_info
            SkuInfoEntity info = this.getById(skuId);
            item.setInfo(info);
            return info;
        }, poolExecutor);
        CompletableFuture<Void> saleAttrsVosFuture = infoFuture.thenAcceptAsync((result) -> {
            //3.当前sku所属的spu下的所有销售属性组合
            List<SkuItemSaleAttrsVo> saleAttrsVos = skuSaleAttrValueService.getSaleAttrsBySpuId(result.getSpuId());
            item.setSaleAttr(saleAttrsVos);
        }, poolExecutor);
        CompletableFuture<Void> infoDescFuture = infoFuture.thenAcceptAsync((res) -> {
            //4.spu的介绍
            SpuInfoDescEntity infoDescEntity = spuInfoDescService.getById(res.getSpuId());
            item.setDesp(infoDescEntity);
        }, poolExecutor);

        CompletableFuture<Void> attrGroupFuture = infoFuture.thenAcceptAsync((res) -> {
            //5.获取spu的规格参数
            List<SpuItemAttrGroupVo> attrGroup = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            item.setGroupAttrs(attrGroup);
        }, poolExecutor);

        //6.查询skuId商品的秒杀预告信息
        CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
            R r = seckillFeignClient.getSkuSeckillInfo(skuId);
            if (r.getCode() == 0) {
                SeckillInfoVo seckillInfoVo = r.getData(new TypeReference<SeckillInfoVo>() {
                });
                item.setSeckillInfoVo(seckillInfoVo);

            }
        }, poolExecutor);

        //等待所有任务都完成
        CompletableFuture.allOf(imageFuture, infoDescFuture, saleAttrsVosFuture, infoDescFuture, attrGroupFuture, seckillFuture).get();

        return item;
    }

    @Override
    public BigDecimal getPrice(Long skuId) {
        SkuInfoEntity byId = getById(skuId);
        if (byId != null) {
            return byId.getPrice();
        }
        return null;
    }

}